/*
 * Copyright (C) 拾光分享网
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai.my.article;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import top.hiai.common.account.UserRoleService;
import top.hiai.common.account.UserService;
import top.hiai.common.model.Article;
import top.hiai.common.model.Comments;
import top.hiai.common.model.Reply;
import top.hiai.common.util.SqlKit;
import top.hiai.index.IndexService;
import top.hiai.my.favorite.MyFavoriteService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @Author www.hiai.top
 * @Email goodsking@163.com
 * @Message 更多资源尽在www.hiai.top
 * @Introduction 类的介绍:个人中心文章Service
 */
public class MyArticleService {

    public static final MyArticleService me = new MyArticleService();
    //分类
    private Comments commentsDao = new Comments().dao();
    private Article articleDao = new Article().dao();
    private Reply replyDao = new Reply().dao();

    /**
     * 遍历所有的分类
     *
     * @return
     */
    public List<Comments> commentsList() {
        return commentsDao.find("select * from bbs_comments order by sort ASC");
    }

    /**
     * 用户发表文章
     *
     * @param accountId
     * @param article
     */
    public void userSaveArticle(int accountId, Article article) {
        article.setUid(accountId);
        article.setAddtime(new Date());
        article.save();
        //发表文章加积分
        UserRoleService.me.setAddUserPoints(UserRoleService.ADD_ARTICLE, accountId);
    }

    /**
     * 删除 文章之前，要删除用户收藏的记录,同时删除文章评论表
     * 管理员调用该方法删除 文章  时，需要先获取一下该 文章  所对应的 accountId，只有这个对上了，才可以删除
     *
     * @param accountId 用户的ID
     * @param aid       文章的ID
     */
    public void myDeleteArticle(final int accountId, final int aid) {

        Db.tx(new IAtom() {
            public boolean run() throws SQLException {
                // 先删除所对应的 评论 列表
                List<Integer> articleReplyIdList = Db.query("select rid from bbs_reply where aid=?", aid);
                //删除文章的评论
                deleteArticleReplyByIdList(articleReplyIdList);

                // 再删除收藏数据
                MyFavoriteService.me.deleteByArticleDeleted(aid);

                // 最后删除 文章
                return Db.update("delete from bbs_article where uid=? and aid=?", accountId, aid) > 0;
            }
        });

        IndexService.me.clearCache();
    }

    /**
     * 删除文章之前，先删除文章的评论
     *
     * @param shareReplyIdList
     */
    public void deleteArticleReplyByIdList(List<Integer> articleReplyIdList) {
        if (articleReplyIdList.size() > 0) {  // idList 有可能为空，不加判断会出异常
            StringBuilder sql = new StringBuilder("delete from bbs_reply where rid in");
            SqlKit.joinIds(articleReplyIdList, sql);
            Db.update(sql.toString());
        }
    }

    /**
     * 根据ID查找文章
     *
     * @param aid
     * @return
     */
    public Article articlefindById(int aid) {
        return articleDao.findById(aid);
    }

    /**
     * 用户修改自己的文章
     *
     * @param accountId
     * @param article
     */
    public void articleUpdate(int accountId, Article article) {
        if (Db.queryInt(" select uid from bbs_article where aid=? limit 1", article.getAid()) != accountId) {
            throw new RuntimeException("个人空间只能操作属于自己的分享");
        }
        article.update();
        IndexService.me.clearCache();
    }

    /**
     * 用户查看自己文章的所有评论列表
     *
     * @param pageNum
     * @param pageSize
     * @param uid
     * @return
     */
    public Page<Reply> myArticleReplyPage(int pageNum, int pageSize, int uid) {
        //select a.aid,a.title,r.uid,r.content,r.addtime from bbs_article as a , bbs_reply as r where a.aid=r.aid and a.uid=1;
        String select = " select a.aid,a.title,r.rid,r.uid,r.content,r.addtime ";
        String from = " from  bbs_article as a , bbs_reply as r where a.aid=r.aid and a.uid=? order by r.addtime desc ";
        Page<Reply> replyList = replyDao.paginate(pageNum, pageSize, select, from, uid);

        //获取评论用户的用户名和头像
        UserService.me.join("uid", replyList.getList(), "nickName", "avatar");
        return replyList;
    }

    /**
     * 用户删除自己文章下面的评论
     *
     * @param rid
     * @param aid
     */
    public void myDeleteArticleReply(int rid, int aid) {
        //删除文章前把文章的评论字段减一
        boolean temp = replyDao.deleteById(rid);
        if (temp) {
            Db.update(" update bbs_article set replyCount = replyCount-1 where aid = ? ", aid);
        }
    }

}